from functools import lru_cache


def stone_game(piles):
    n = len(piles)

    @lru_cache(None)
    def dp(i, j):
        nonlocal n
        # base
        if i > j: return 0
        alex = (j - i - n) % 2 == 1
        if alex:
            return max(piles[i] + dp(i + 1, j), piles[j] + dp(i, j + 1))
        else:
            return min(-piles[i] + dp(i + 1, j), -piles[j] + dp(i, j + 1))

    # n = j - i + 1, j - i - n = -1, odd num->alex,
    return dp(0, n - 1) > 0




